﻿(function QDateBox(window, undefined){
	
	/**
	 * 保存window对象
	 */
	var document = window.document,
		_QDateBox = window.QDateBox,
		QDateBox = {},
		List = [];
	
	window.QDateBox = QDateBox;
	
	/**
	 * 配置
	 */
	QDateBox.option = {
		type : "listbox",
		yearCss : {
			"width" : "90px",
			"float" : "left"
		},
		monthCss : {
			"width" : "90px",
			"float" : "left"
		},
		dayCss : {
			"width" : "90px",
			"float" : "left",
			"margin-right" : 0
		}
	}
	
	function dateBox(_DOM, _option){
		
		var $DOM = _DOM || $("<div>"),	//主键
			option = $.extend(true, {}, QDateBox.option),	// 深层复制配置
			yearList,
			monthList,
			dayList;
		
		if(_option) option = $.extend(true, option, _option);
		
		if(option.type == "listbox"){
			
			yearList = QListBox.init("<div>", {defaultItem:{text:"年",value:-1}});
			yearList.getDOM().css(option.yearCss);
			yearList.onSelected(function(_value){
				
				var tmpYear = yearList.getValue(),
				tmpMonth = monthList.getValue();
			
				if(tmpMonth == -1){
					dayList.clear();
				} else if(tmpMonth == 2) {

					var dayCount = new Date(tmpYear, tmpMonth,0).getDate(),
						tmpList = [];
					for(var i=1; i<=dayCount; i++){
						tmpList.push(i);
					}
					
					dayList.refresh(tmpList);
				}
			});
			
			monthList = QListBox.init("<div>", {defaultItem:{text:"月",value:-1}});
			monthList.getDOM().css(option.monthCss);
			monthList.onSelected(function(_value){
				
				var tmpYear = yearList.getValue(),
				tmpMonth = monthList.getValue();
			
				if(tmpMonth == -1){
					dayList.clear();
				} else {
					var dayCount = new Date(tmpYear, tmpMonth,0).getDate(),
						tmpList = [];
					for(var i=1; i<=dayCount; i++){
						tmpList.push(i);
					}
					
					dayList.refresh(tmpList);
				}
			});
			
			
			dayList = QListBox.init("<div>", {defaultItem:{text:"日",value:-1}});
			dayList.getDOM().css(option.dayCss);
			
			$DOM.append(yearList.getDOM());
			$DOM.append(monthList.getDOM());
			$DOM.append(dayList.getDOM());
		}
		
		this.clear = function(){
			yearList.clear();
			monthList.clear();
			dayList.clear();
		}
		
		this.refresh = function(_data){
			
			if(!_data) return this;
			
			this.clear();
			var date = new Date(_data);
			var year = date.getFullYear(),
				list = [];
			for(var i=year; i>=1900; i--){
				list.push(i);
			}
			
			yearList.refresh(list);
			
			list = [];
			for(var i=1; i<=12; i++){
				list.push(i);
			}
			monthList.refresh(list);
		}
		
		this.setValue = function(_value){
			
			var date = new Date(_value);
			yearList.setValue(date.getFullYear());
			monthList.setValue(date.getMonth() + 1);
			dayList.setValue(date.getDate());
		}
		
		this.getValue = function(){
			
			var year = yearList.getValue(),
				month = monthList.getValue(),
				day = dayList.getValue();
			
			if(year == -1 && month == -1 && day == -1){
				return -1;
			}
			
			month = month > 9 ? month : "0" + month;
			day = day > 9 ? day : "0" + day;
			
			return year + "-" + month + "-" + day;
		}
	};
	
	QDateBox.init = function(_pk, _option){
		
		var $DOM = $(_pk);							//传入对象可以是jquery对象，或者是表达式
		var box = new dateBox($DOM, _option);
		List[_pk] = List.length;				//索引，其实没必要，直接用对象的map性质存储也是可以的，这里为了让代码更好理解
		List.push(box);							//保存创建的对象
		return box;						//初始化完成，返回对象
	}
	
	/**
	 * 获取tip
	 */
	QDateBox.get = function(_pk){
		
		return List[List[_pk]];
	}
})(window);